package com.study.ThreadStop;

public class StopThread implements Runnable {
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println("当前线程执行了:" + i++);
        }
        System.out.println("停止了");
    }

    public static void main(String[] args) {
        StopThread thread = new StopThread();
        new Thread(thread).start();
        if ("main".equals(Thread.currentThread().getName())) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = 0; i < 1000; i++) {
            if (i == 900) {
                // 自定义的不是系统的
                thread.stop();
            }
        }

    }

    /**
     * 改变标志
     */
    public void stop() {
        this.flag = false;
    }

}
